package com.ehsunbehravesh.service.security.service;

import com.ehsunbehravesh.service.security.ejb.hash.HashLocalBusiness;
import com.ehsunbehravesh.service.security.service.jaxb.BaseResult;
import com.ehsunbehravesh.service.security.service.jaxb.HashRequestResponse;
import com.ehsunbehravesh.service.security.service.jaxb.SumResponse;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author ehsun.behravesh
 */
@Stateless
@LocalBean
@Path("hash")
public class HashService {

  @EJB
  HashLocalBusiness hashBusiness;

  @POST
  @Path("md5")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public HashRequestResponse hashMD5(HashRequestResponse request) {
    HashRequestResponse response = new HashRequestResponse();

    try {
      byte[] hash = hashBusiness.hashMD5(request.getData());
      response.setData(hash);
      response.setStatus(BaseResult.Status.SUCCESS);
    } catch (Exception ex) {
      response.setStatus(BaseResult.Status.FAILURE);
      response.setMessage(ex.getMessage());
    }

    return response;
  }
  
  @POST
  @Path("sha1")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public HashRequestResponse hashSHA1(HashRequestResponse request) {
    HashRequestResponse response = new HashRequestResponse();

    try {
      byte[] hash = hashBusiness.hashSHA1(request.getData());
      response.setData(hash);
      response.setStatus(BaseResult.Status.SUCCESS);
    } catch (Exception ex) {
      response.setStatus(BaseResult.Status.FAILURE);
      response.setMessage(ex.getMessage());
    }

    return response;
  }
  
  @POST
  @Path("sha256")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public HashRequestResponse hashSHA256(HashRequestResponse request) {
    HashRequestResponse response = new HashRequestResponse();

    try {
      byte[] hash = hashBusiness.hashSHA256(request.getData());
      response.setData(hash);
      response.setStatus(BaseResult.Status.SUCCESS);
    } catch (Exception ex) {
      response.setStatus(BaseResult.Status.FAILURE);
      response.setMessage(ex.getMessage());
    }

    return response;
  }
  
  @POST
  @Path("sha512")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public HashRequestResponse hashSHA512(HashRequestResponse request) {
    HashRequestResponse response = new HashRequestResponse();

    try {
      byte[] hash = hashBusiness.hashSHA512(request.getData());
      response.setData(hash);
      response.setStatus(BaseResult.Status.SUCCESS);
    } catch (Exception ex) {
      response.setStatus(BaseResult.Status.FAILURE);
      response.setMessage(ex.getMessage());
    }

    return response;
  }
  
  @POST
  @Path("crc32")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public SumResponse sumCRC32(HashRequestResponse request) {
    SumResponse response = new SumResponse();

    try {
      long result = hashBusiness.sumCRC32(request.getData());
      response.setData(result);
      response.setStatus(BaseResult.Status.SUCCESS);
    } catch (Exception ex) {
      response.setStatus(BaseResult.Status.FAILURE);
      response.setMessage(ex.getMessage());
    }

    return response;
  }
  
  @POST
  @Path("adler32")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public SumResponse sumAdler32(HashRequestResponse request) {
    SumResponse response = new SumResponse();

    try {
      long result = hashBusiness.sumAdler32(request.getData());
      response.setData(result);
      response.setStatus(BaseResult.Status.SUCCESS);
    } catch (Exception ex) {
      response.setStatus(BaseResult.Status.FAILURE);
      response.setMessage(ex.getMessage());
    }

    return response;
  }
}
